package ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.sxpjyy.monkey.mobilenurse.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import adapters.CostPagerAdapter;
import mytools.MyBaseActivity;
import objects.CostItem;
import objects.InPatient;

public class CostActivity extends MyBaseActivity implements ViewPager.OnPageChangeListener {

    private CostPagerAdapter mCostPagerAdapter;

    private ViewPager mViewPager;

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private List<CostItem> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cost);
        onBindUI();
        setSupportActionBar(toolbar);
        mCostPagerAdapter = new CostPagerAdapter(getSupportFragmentManager(), this);
        mViewPager.setAdapter(mCostPagerAdapter);
        tabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(this);
    }

    private void onBindUI() {
        toolbar = findViewById(R.id.toolbar);
        mViewPager = findViewById(R.id.container);
        tabLayout = findViewById(R.id.tab_layout);
    }

    @Override
    protected void onResume() {
        super.onResume();
        InPatient inPatient = getMyApplication().getInPatient();
        if (inPatient != null) {
            toolbar.setTitle(inPatient.getBRXM());
        }

        list = new ArrayList<>();
        for (int i = 1; i < 20; i++) {
            CostItem costItem = new CostItem();
            costItem.setCost_name("费用" + i);
            DecimalFormat decimalFormat = new DecimalFormat("######0.00");
            costItem.setCost_quantity(decimalFormat.format(100 - i * 2));
            costItem.setCost_byselt(decimalFormat.format((100 - i * 2) * 0.77));
            list.add(costItem);
        }
//        mCostPagerAdapter.setCostItems(list);
//        mCostPagerAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_money, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                mCostPagerAdapter.setCostItems(list);
                mCostPagerAdapter.notifyDataSetChanged();
                break;
//            case 1:
//                break;
//            case 2:
//                break;
            default:
                Toast.makeText(CostActivity.this, position + " selected", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

//    /**
//     * A placeholder fragment containing a simple view.
//     */
//    public static class CostPlaceholderFragment extends Fragment {
//
//        private static Context mContext;
//        private static List<CostItem> costItemList;
//
//        /**
//         * The fragment argument representing the section number for this
//         * fragment.
//         */
//        private static final String ARG_SECTION_NUMBER = "section_number";
//
//        public CostPlaceholderFragment() {
//        }
//
//        public static void setmContext(Context context) {
//            mContext = context;
//        }
//
//        public static void setList(List<CostItem> list) {
//            costItemList = list;
//        }
//
//        private String getTotalNum() {
//            double total = 0;
//            if (costItemList != null && costItemList.size() > 0) {
//                for (CostItem costItem : costItemList
//                        ) {
//                    total += Double.valueOf(costItem.getCost_quantity());
//                }
//                return String.valueOf(total);
//            } else {
//                return null;
//            }
//        }
//
//        /**
//         * Returns a new instance of this fragment for the given section
//         * number.
//         */
//        public static CostPlaceholderFragment newInstance(int sectionNumber) {
//            CostPlaceholderFragment fragment = new CostPlaceholderFragment();
//            Bundle args = new Bundle();
//            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
//            fragment.setArguments(args);
//            return fragment;
//        }
//
//        @Override
//        public View onCreateView(LayoutInflater inflater, ViewGroup parent,
//                                 Bundle savedInstanceState) {
//            int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
//            View rootView;
//            switch (sectionNumber) {
//                case 1:
//
//                    rootView = inflater.inflate(R.layout.fragment_cost_1, parent, false);
//                    ListView listView = (ListView) rootView.findViewById(R.id.cost_item_list);
//                    TextView textNum = (TextView) rootView.findViewById(R.id.total_num_text);
//                    TextView textChs = (TextView) rootView.findViewById(R.id.total_chs_text);
//                    CostItemAdapter costItemAdapter = new CostItemAdapter(mContext);
//                    listView.setAdapter(costItemAdapter);
//                    if (costItemList != null && costItemList.size() > 0) {
//                        costItemAdapter.setList(costItemList);
//                        costItemAdapter.notifyDataSetChanged();
//                    }
//                    String total = getTotalNum();
//                    textNum.setText(total);
//                    textChs.setText(Trans2RMB.getRMB(total));
//
//                    break;
////                case 2:
////                    break;
////                case 3:
////                    break;
//                default:
//                    rootView = inflater.inflate(R.layout.fragment_money, parent, false);
//                    TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//                    textView.setText(getString(R.string.section_format, sectionNumber));
//                    break;
//            }
//
//
//            return rootView;
//        }
//    }

}
